package com.yuyou.fn.common.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by wens on 16-7-22.
 */
public class TimeUtils {

    public static String prettyTime(long off) {

        off = off / 1000;
        if (off < 60) {
            return off + "秒";
        }

        off = off / 60;

        if (off < 60) {
            return off + "分钟";
        }

        off = off / 60;

        if (off < 24) {
            return off + "小时";
        }

        return off / 24 + "天";


    }

    public static Date now() {
        return new Date();
    }

    /**
     * 时间转换格式
     * @param date
     * @return
     */
    public static String date2String(Date date) {
        if(date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strDate = sdf.format(date);
            return strDate;
        }
        return null;
    }

    public static String date2String(Date date ,String format) {
        if(date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String strDate = sdf.format(date);
            return strDate;
        }
        return null;
    }

    public static Date parse(String src, String format)  {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(src) ;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    /*两个日期相隔天数*/
    public static int daysOfTwo(Date fDate, Date oDate) {

        Calendar aCalendar = Calendar.getInstance();

        aCalendar.setTime(fDate);

        int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

        aCalendar.setTime(oDate);

        int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

        return day2 - day1;

    }


    /**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static int getWeekOfDate(Date dt) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return  w;
    }

    public static boolean isOverlap(String startdate1, String enddate1,String startdate2, String enddate2,String timeFormat) {
        SimpleDateFormat format = new SimpleDateFormat(timeFormat);
        Date leftStartDate = null;
        Date leftEndDate = null;
        Date rightStartDate = null;
        Date rightEndDate = null;
        try {
            leftStartDate = format.parse(startdate1);
            leftEndDate = format.parse(enddate1);
            rightStartDate = format.parse(startdate2);
            rightEndDate = format.parse(enddate2);
        } catch (ParseException e) {
            return false;
        }
        return        ((leftStartDate.getTime() >= rightStartDate.getTime()) && leftStartDate.getTime() < rightEndDate.getTime())
                        || ((leftStartDate.getTime() > rightStartDate.getTime())
                                && leftStartDate.getTime() <= rightEndDate.getTime())
                        ||
                        ((rightStartDate.getTime() >= leftStartDate.getTime())
                                && rightStartDate.getTime() < leftEndDate.getTime())
                        ||
                        ((rightStartDate.getTime() > leftStartDate.getTime())
                                && rightStartDate.getTime() <= leftEndDate.getTime());

    }

    //给定两个日期（string），遍历之间的所有日期
    public static List<String> queryData(String startAt, String endAt) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dates = new ArrayList<>();
        try {
            Date startDate = dateFormat.parse(startAt);
            Date endDate = dateFormat.parse(endAt);
            dates.addAll(queryData(startDate, endDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dates;
    }

    public static List<String> queryData(Date startAt, Date endAt) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<String> dates = new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(startAt);
        Calendar end = Calendar.getInstance();
        end.setTime(endAt);
        while (start.before(end) || start.equals(end)) {
            dates.add(dateFormat.format(start.getTime()));
            start.add(Calendar.DAY_OF_YEAR, 1);
        }
        return dates;
    }

    public static BigDecimal hoursCounting(String startDate, String endDate, String startTime, String endTime,String dateFormat,String timeFormat) {
        BigDecimal ret = new BigDecimal(0);
        Date date1 = parse(startDate, dateFormat);
        Date date2 = parse(endDate, dateFormat);
        BigDecimal hours1 = hoursOfTwo(date1, date2);
        Date time1 = parse(startTime, timeFormat);
        Date time2 = parse(endTime, timeFormat);
        BigDecimal hours2 = hoursOfTwo(time1,time2);
        return hours1.add(hours2);
    }

    //相隔小时数
    private static BigDecimal hoursOfTwo(Date time1, Date time2) {

        long diff = time2.getTime() - time1.getTime();
        BigDecimal hours = new BigDecimal(diff).divide(new BigDecimal(3600000),2,BigDecimal.ROUND_HALF_UP);
        return hours;
    }

    public  static  void main(String[] art){
        Date date1=new Date();
        Date date2=new Date();
        System.out.println(getWeekOfDate(date1));

      /*  System.out.print(isOverlap(date1,date2,date3,date4));*/
    }

}
